import 'package:flutter/material.dart';
import 'package:azlistview/azlistview.dart';
import 'package:lpinyin/lpinyin.dart';

import '../models/city_info.dart';

class CitySelect extends StatefulWidget {
  final List<CityInfo> cityList;
  final List<CityInfo> hotCityList;
  final CityInfo currentCity;

  CitySelect({this.currentCity, this.cityList, this.hotCityList});

  @override
  State<StatefulWidget> createState() {
    return new _CitySelectState();
  }
}

class _CitySelectState extends State<CitySelect> {
  List<CityInfo> _cityList = List();
  List<CityInfo> _hotCityList = List();

  int _suspensionHeight = 40;
  int _itemHeight = 50;
  String _suspensionTag = "";

  @override
  void initState() {
    super.initState();
    _handleHotCityList(widget.hotCityList);
    _handleCityList(widget.cityList);
  }

  void _handleCityList(List<CityInfo> list) {
    if (list == null || list.isEmpty) return;

    for (int i = 0, length = list.length; i < length; i++) {
      String pinyin = PinyinHelper.getPinyinE(list[i].name);
      String tag = pinyin.substring(0, 1).toUpperCase();

      CityInfo info = CityInfo(
        name: list[i].name,
        namePinyin: pinyin,
        tagIndex: RegExp("[A-Z]").hasMatch(tag) ? tag : '#',
      );

      _cityList.add(info);
    }

    //根据A-Z排序
    SuspensionUtil.sortListBySuspensionTag(_cityList);
  }

  void _handleHotCityList(List<CityInfo> list) {
    if (list == null || list.isEmpty) return;
    for (int i = 0, length = list.length; i < length; i++) {
      CityInfo info = CityInfo(
        name: list[i].name,
        tagIndex: '★',
      );

      _hotCityList.add(info);
    }

    _suspensionTag = _hotCityList[0].getSuspensionTag();
  }

  void _onSusTagChanged(String tag) {
    setState(() {
      _suspensionTag = tag;
    });
  }

  Widget _buildSusWidget(String susTag) {
    susTag = (susTag == "★" ? "热门城市" : susTag);
    return Container(
      height: _suspensionHeight.toDouble(),
      padding: const EdgeInsets.only(left: 15.0),
      decoration: BoxDecoration(
        color: Color(0xfff3f4f5),
      ),
      alignment: Alignment.centerLeft,
      child: Text(
        '$susTag',
        softWrap: false,
        style: TextStyle(
          fontSize: 14.0,
          color: Color(0xff999999),
        ),
      ),
    );
  }

  Widget _buildListItem(CityInfo model) {
    String susTag = model.getSuspensionTag();
    susTag = (susTag == "★" ? "热门城市" : susTag);
    return Column(
      children: <Widget>[
        Offstage(
          offstage: model.isShowSuspension != true,
          child: _buildSusWidget(susTag),
        ),
        SizedBox(
          height: _itemHeight.toDouble(),
          child: ListTile(
            title: Text(model.name),
            onTap: () {
              print("OnItemClick: $model");
              Navigator.pop(context, model);
            },
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final String currentCityName = widget.currentCity?.name ?? '';

    return Column(
      children: <Widget>[
        Container(
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.only(left: 15.0),
          height: 50.0,
          child: Text("当前城市: $currentCityName"),
        ),
        Expanded(
          flex: 1,
          child: AzListView(
            data: _cityList,
            topData: _hotCityList,
            itemBuilder: (context, model) => _buildListItem(model),
            suspensionWidget: _buildSusWidget(_suspensionTag),
            isUseRealIndex: true,
            itemHeight: _itemHeight,
            suspensionHeight: _suspensionHeight,
            onSusTagChanged: _onSusTagChanged,
          ),
        ),
      ],
    );
  }
}
